package com.sun.module_common.widget

import androidx.compose.foundation.layout.width
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.airbnb.lottie.compose.LottieAnimation
import com.airbnb.lottie.compose.LottieCompositionSpec
import com.airbnb.lottie.compose.LottieConstants
import com.airbnb.lottie.compose.rememberLottieComposition
import com.airbnb.lottie.compose.rememberLottieRetrySignal
import com.sun.module_network.utils.logi

/**
 *@author:sunc
 *@time :2025/8/19 9:17
 *@desc: 描述 动画控件
 */

@Composable
fun AnimationView(
    spec: LottieCompositionSpec,
    iterations: Int = LottieConstants.IterateForever,
    modifier: Modifier = Modifier.width(200.dp)
) {
    //解析动画文件
    val retrySignal = rememberLottieRetrySignal()
    val lottieComposition by if (spec is LottieCompositionSpec.Url) { //网络图片添加失败重 试
        rememberLottieComposition(spec = spec, onRetry = { failCount, previousException ->
            if (failCount > 3) {
                "错误信息-->${previousException.message}".logi()
                false
            } else {
                retrySignal.awaitRetry()
                true
            }
        })
    } else {
        rememberLottieComposition(spec = spec)
    }

    LottieAnimation(
        composition = lottieComposition,
        iterations = iterations,
        modifier = modifier
    )
}